package com.lichuang.dedao.city.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.api.R;
import com.lichuang.dedao.city.fingnclient.UserFeignClient;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
public class Reply {

    public Reply(){

    }

    /**
     *
     *构造函数
     * @param note
     */
    public Reply(Comment note){
        this.commentId = note.getCommentId();
        this.notrId = note.getNotrId();
        this.commentParentId = note.getCommentParentId();
        this.commentContent = note.getCommentContent();
        this.commentUserId = note.getCommentUserId();
        this.commentTime = note.getCommentTime();
        this.img = note.getImg();
        this.username = note.getUsername();
    }
    @ApiModelProperty(value = "评论id")
    @TableId(value = "comment_id", type = IdType.AUTO)
    private Integer commentId;

    @ApiModelProperty(value = "note_id")
    private Integer notrId;

    @ApiModelProperty(value = "评论内容")
    private String commentContent;

    @ApiModelProperty(value = "评论父级id 为0则没有父级")
    private Integer commentParentId;

    @ApiModelProperty(value = "user id")
    private Integer commentUserId;

    @ApiModelProperty(value = "评论时间")
    private LocalDateTime commentTime;

    @ApiModelProperty(value = "用户头像")
    @TableField(exist = false)
    private String img;

    @ApiModelProperty("回复用户名")
    @TableField(exist = false)
    private String username;

    //下一条回复
    private List<Reply> nextNodes = new ArrayList<Reply>();

        /**
         * 将单个node添加到链表中
         * @param list
         * @param node
         * @return
         */
        public static boolean addNode(List<Reply> list,Reply node){
            for (Reply node1 : list) {   //循环添加
                if (node1.getCommentId()==node.getCommentParentId()){   //判断留言的上一段是都是这条留言
                    node1.getNextNodes().add(node);   //是，添加，返回true;
                    System.out.println("添加了一个");
                    return true;
                }else{     //否则递归继续判断
                    if (node1.getNextNodes().size()!=0)
                        if (Reply.addNode(node1.getNextNodes(),node)){
                            return true;
                        }
                }
            }
            return false;
        }

    @Autowired
    static UserFeignClient userFeignClient;
        /**
         * 将查出来的lastId不为null的回复都添加到第一层Node集合中
         * @param firstList
         * @param thenList
         * @return
         */
        public static List addAllNode(List<Reply> firstList,List thenList){
            while (thenList.size()!=0){
                int size = thenList.size();
                for (int i = 0;i<size;i++){
                    if (Reply.addNode(firstList,new Reply((Comment) thenList.get(i)))){
                        thenList.remove(i);
                        i--;
                        size--;
                    }
                }
            }
            return firstList;
        }

        //打印
        public static void show(List<Reply> list){
            for (Reply node : list) {
                System.out.println(node.getCommentUserId()+" 用户回复了你："+node.getCommentContent());
                if (node.getNextNodes().size()!=0){
                    Reply.show(node.getNextNodes());
                }
            }
        }
}
